package junk;
import java.util.ArrayList;


import processing.core.PApplet;


public class Events extends Main1{
	private static final long serialVersionUID = 1L;
	public void mouseClickedEvent(PApplet p)
	{
		float innerRadius1 = (HEIGHT1/p.height)*innerRadius; 
		float mouseX1 = (WIDTH1/p.width)*p.mouseX;
		float mouseY1 = (HEIGHT1/p.height)*p.mouseY;
		//float xTranslate1 = (WIDTH1/p.width)*xTranslate;
		p.scale(p.width/WIDTH1,p.height/HEIGHT1);
		
		//manage data button
		if(mouseY1- HEIGHT1/2 > 520 && mouseY1 - HEIGHT1/2 < 700)
			if(mouseX1 + xTranslate1 > WIDTH1-80 && mouseX1 + xTranslate1 < WIDTH1+120)
			{
				frontEnd = !frontEnd;
				p.redraw();
				return;
			}
		
		//show full set trace
		if(!frontEnd && mouseY1- HEIGHT1*.9f > 12.5 && mouseY1 - HEIGHT1*.9f < 37.5)
			if(mouseX1 + xTranslate1 > WIDTH1*.75f-237.5 && mouseX1 + xTranslate1 < WIDTH1*.75f-212.5f)
			{
				Backend.showFullSet = !Backend.showFullSet;
				p.redraw();
				return;
			}
		
		
		if(newAnnotationPopup)
		{			
			if((mouseX1 - WIDTH1/2 + xTranslate1 > innerRadius1 - 275 - 65) && (mouseX1 - WIDTH1/2 + xTranslate1 < innerRadius1 - 275 + 65))
			{
				//more groups
				if((mouseY1 - HEIGHT1/2 > 60 - 20) && (mouseY1 - HEIGHT1/2 < 60 + 20))
				{
					int[] groupCutoffCopy = groupCutoff;
					groupCutoff = new int[groupCutoffCopy.length + 1];
					for(int i = 0; i< groupCutoffCopy.length; i++)
						groupCutoff[i] = groupCutoffCopy[i];
					groupCutoff[groupCutoffCopy.length] = 100;
					float[] groupCutoffRawCopy = groupCutoffRaw;
					groupCutoffRaw = new float[groupCutoffRawCopy.length + 1];
					for(int i = 0; i< groupCutoffRawCopy.length; i++)
						groupCutoffRaw[i] = groupCutoffRawCopy[i];
					groupCutoffRaw[groupCutoffRawCopy.length] = 100;
				}
				//fewer groups
				if((mouseY1 - HEIGHT1/2 > 110 - 20) && (mouseY1 - HEIGHT1/2 < 110 + 20) && groupCutoff.length > 1)
				{
					int[] groupCutoffCopy = groupCutoff;
					groupCutoff = new int[groupCutoffCopy.length - 1];
					for(int i = 0; i< groupCutoffCopy.length-1; i++)
						groupCutoff[i] = groupCutoffCopy[i];
					float[] groupCutoffRawCopy = groupCutoffRaw;
					groupCutoffRaw = new float[groupCutoffRawCopy.length - 1];
					for(int i = 0; i< groupCutoffRawCopy.length-1; i++)
						groupCutoffRaw[i] = groupCutoffRawCopy[i];
				}
			}
			//distribution selection
			if((mouseX1 - WIDTH1/2 + xTranslate1 > innerRadius1 - 375) && (mouseX1 - WIDTH1/2 + xTranslate1 < innerRadius1 - 175))
			{
				if((mouseY1 - HEIGHT1/2 > 215) && (mouseY1 - HEIGHT1/2 < 255))
					percentValues = false;
				if((mouseY1 - HEIGHT1/2 > 255) && (mouseY1 - HEIGHT1/2 < 295))
					percentValues = true;
			}
			if((mouseY1 - HEIGHT1/2 > innerRadius1 - 200 + 20) && (mouseY1 - HEIGHT1/2 < innerRadius1 - 200 + 60))
			{
				//cancel
				if((mouseX1 - WIDTH1/2 + xTranslate1 > innerRadius1 - 350 - 50) && (mouseX1 - WIDTH1/2 + xTranslate1 < innerRadius1 - 350 + 50))
				{
					newAnnotationPopup = false;
					Protein.drawLines = true;
				}
				//create
				if((mouseX1 - WIDTH1/2 + xTranslate1 > -innerRadius1 + 350 - 50) && (mouseX1 - WIDTH1/2 + xTranslate1 < -innerRadius1 + 350 + 50))
				{
					Annotations.createNewAnnotation(newAnnotationGroup, groupCutoffRaw, names, p);
					newAnnotationPopup = false;
					Protein.drawLines = true;
				}
			}
			p.scale(WIDTH1/width, HEIGHT1/height);
			p.redraw();
			return;
		}
		if(booleanGroupPopup)
		{
			if((mouseY1 - HEIGHT1/2 > bool1.y1 - 240) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 210))
			{
				if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 420) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 340))
				{
					b1 = true;
					b2 = false;
				}
				else if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 330) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 235))
				{
					b2 = true;
					b1 = false;
				}
			}
			if((mouseY1 - HEIGHT1/2 > bool1.y1 - 200) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 170))
			{
				if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 320) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 230))
				{
					b3 = true;
					b4 = false;
				}
				else if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 210) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 130))
				{
					b4 = true;
					b3 = false;
				}
			}
			if((mouseY1 - HEIGHT1/2 > bool1.y1 - 160) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 130))
			{
				if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 420) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 340))
				{
					b5 = true;
					b6 = false;
				}
				else if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 330) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 235))
				{
					b6 = true;
					b5 = false;
				}
			}
			if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 225) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 195))
			{
				if((mouseY1 - HEIGHT1/2 > bool1.y1 - 242) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 227))
				{
					b7 = true;
					b8 = false;
				}
				if((mouseY1 - HEIGHT1/2 > bool1.y1 - 224) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 209))
				{
					b8 = true;
					b7 = false;
				}
				if((mouseY1 - HEIGHT1/2 > bool1.y1 - 162) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 147))
				{
					b9 = true;
					b10 = false;
				}
				if((mouseY1 - HEIGHT1/2 > bool1.y1 - 144) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 129))
				{
					b10 = true;
					b9 = false;
				}				
			}
			if((mouseY1 - HEIGHT1/2 > bool1.y1 - 75) && (mouseY1 - HEIGHT1/2 < bool1.y1 - 35))
			{
				if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 370) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 270))
				{
					String op;
					if(b3) op = "AND";
					else op = "OR";
					if(b1==b2 || b3==b4 || b5==b6) 
					{
						p.scale(WIDTH1/width, HEIGHT1/height);
						p.redraw();
						return;
					}
					int s1, s2;
					if(b7==b8) s1 = 0;
					else if(b7) s1 = 1;
					else s1 = -1;
					if(b9==b10) s2 = 0;
					else if(b9) s2 = 1;
					else s2 = -1;
					new BooleanGroup(b2, s1, bool1, op, b6, s2, bool2,p);
					booleanGroupPopup= false;
				}
				else if((mouseX1 - WIDTH1/2 + xTranslate1 > bool1.x1 - 170) && (mouseX1 - WIDTH1/2 + xTranslate1 < bool1.x1 - 70))
				{
					booleanGroupPopup= false;
				}
			}
			p.scale(WIDTH1/width, HEIGHT1/height);
			p.redraw();
			return;
				
		}
		if((mouseY1 > 118) && (mouseY1  < 138) && (mouseX1 > 23) && (mouseX1 < 43))
		{
			uniformGeneSize = !uniformGeneSize;
			updateRings = true;
			firstAnnotation = new Annotations(annotationOrder[0],"All", "All", firstAnnotation.genes, p, 1);
			p.scale(WIDTH1/width, HEIGHT1/height);
			p.redraw();
			return;
		}


		  float lengthFromOrigin = Main1.dist(p.mouseX,p.mouseY,p.width/2-xTranslate,p.height/2);	  
		  //checks if user is clicking in the gene ring
		  //first narrows it down to a subgroup to limit the number of genes it checks
		  if (lengthFromOrigin < innerRadius && lengthFromOrigin > innerRadius*.98f)
		  {
			  float mouseAngle = (float)(Math.atan2(-1*(p.mouseY - p.height/2),(p.mouseX - p.width/2 + xTranslate)));
			  if (mouseAngle< 0) mouseAngle += 2*PI;
			  if (mouseAngle< PI/2) mouseAngle += 2*PI;
			  for( int i = 0; i< firstAnnotation.numChildren; i++)
			  {
				  if (mouseAngle > firstAnnotation.bounds[i][0] && mouseAngle < firstAnnotation.bounds[i][1])
				  {
    				  if (mouseAngle > firstAnnotation.bounds[i][0] && mouseAngle < firstAnnotation.bounds[i][1])
    				  {
    					  ArrayList a = (ArrayList)firstAnnotation.subGroups.get(i);
    					  for(int k = 0; k < a.size(); k++)
    					  {
    						  Gene gene = (Gene)a.get(k);
    						  gene.checkMouseOver();
    					  }
    				  }
    				  p.scale(WIDTH1/width, HEIGHT1/height);
    				  p.redraw();
    				  return;  
			      }
			  }
		  }	 
	  
		  //handles when user clicks arrows at bottom to change the ring orientation
		  if(mouseY1 > HEIGHT1 - 15)
		  {
			  for(int i = firstAnnotation.myLevels; i< annotationOrder.length; i++)
				{
					float offset = (i- firstAnnotation.myLevels)*75;
					if(mouseX1 > offset+30 && mouseX1 < offset+40)
					{
						String temp = annotationOrder[0];
						annotationOrder[0] = annotationOrder[i];
						annotationOrder[i] = temp;				
						String[] temp1 = annotationSubGroups[0];
						annotationSubGroups[0] = annotationSubGroups[i];
						annotationSubGroups[i] = temp1;				
						int temp2 = annotationColumn[0];
						annotationColumn[0] = annotationColumn[i];
						annotationColumn[i] = temp2;
						
						Annotations firstAnnotationsCopy = firstAnnotation;
						firstAnnotation = new Annotations(annotationOrder[0],"All", "All", firstAnnotationsCopy.genes, p, 1);
						//Annotations.setLevels(firstAnnotation, firstAnnotationsCopy);
						//firstAnnotation.myLevels = firstAnnotationsCopy.myLevels;
					}
				}
			  updateRings = true;
			  p.scale(WIDTH1/width, HEIGHT1/height);
			  p.redraw();
			  return;
		  }
		  if(mouseY1 < HEIGHT1 - 50 && mouseY1 > HEIGHT1 - 75)
		  {
			  for(int i = firstAnnotation.myLevels; i< annotationOrder.length; i++)
				{
					float offset = (i- firstAnnotation.myLevels)*75;
					if(mouseX1 > offset+30 && mouseX1 < offset+40)
					{
						String temp = annotationOrder[1];
						annotationOrder[1] = annotationOrder[i];
						annotationOrder[i] = temp;				
						String[] temp1 = annotationSubGroups[1];
						annotationSubGroups[1] = annotationSubGroups[i];
						annotationSubGroups[i] = temp1;				
						int temp2 = annotationColumn[1];
						annotationColumn[1] = annotationColumn[i];
						annotationColumn[i] = temp2;
						
						Annotations firstAnnotationsCopy = firstAnnotation;
						firstAnnotation = new Annotations(annotationOrder[0],"All", "All", firstAnnotationsCopy.genes, p, 1);
						firstAnnotation = new Annotations(firstAnnotation, 2);	
						//Annotations.setLevels(firstAnnotation, firstAnnotationsCopy);
						//firstAnnotation.myLevels = firstAnnotationsCopy.myLevels;
					}
				}
			  //swap outer and middle ring
			  /*
			  if ((mouseX1 < 40 && mouseX1 > 30) || (mouseX1 > 100 && mouseX1 < 110))
			  {
				String temp = annotationOrder[0];
				annotationOrder[0] = annotationOrder[1];
				annotationOrder[1] = temp;				
				String[] temp1 = annotationSubGroups[0];
				annotationSubGroups[0] = annotationSubGroups[1];
				annotationSubGroups[1] = temp1;				
				int temp2 = annotationColumn[0];
				annotationColumn[0] = annotationColumn[1];
				annotationColumn[1] = temp2;
				
				Annotations firstAnnotationsCopy = firstAnnotation;
				firstAnnotation = new Annotations(annotationOrder[0],"All", "All", firstAnnotationsCopy.genes, p, 1);
				//Annotations.setLevels(firstAnnotation, firstAnnotationsCopy);
			  }
			  //swap two middle rings
			  if ((mouseX1 > 170 && mouseX1 < 190) || (mouseX1 > 110 && mouseX1 < 120))
			  {
				String temp = annotationOrder[1];
				annotationOrder[1] = annotationOrder[2];
				annotationOrder[2] = temp;
				String[] temp1 = annotationSubGroups[1];
				annotationSubGroups[1] = annotationSubGroups[2];
				annotationSubGroups[2] = temp1;				
				int temp2 = annotationColumn[1];
				annotationColumn[1] = annotationColumn[2];
				annotationColumn[2] = temp2;
				
				Annotations firstAnnotationsCopy = firstAnnotation;
				firstAnnotation = new Annotations(annotationOrder[0],"All", "All", firstAnnotationsCopy.genes, p, 1);
				//Annotations.setLevels(firstAnnotation, firstAnnotationsCopy);
			  }
			//swap middle and inner ring
			  if ((mouseX1 > 190 && mouseX1 < 200) || (mouseX1 > 260 && mouseX1 < 270))
			  {
				String temp = annotationOrder[2];
				annotationOrder[2] = annotationOrder[3];
				annotationOrder[3] = temp;
				String[] temp1 = annotationSubGroups[2];
				annotationSubGroups[2] = annotationSubGroups[3];
				annotationSubGroups[3] = temp1;				
				int temp2 = annotationColumn[2];
				annotationColumn[2] = annotationColumn[3];
				annotationColumn[3] = temp2;
				Annotations firstAnnotationsCopy = firstAnnotation;
				firstAnnotation = new Annotations(annotationOrder[0],"All", "All", firstAnnotationsCopy.genes, p, 1);
				//Annotations.setLevels(firstAnnotation, firstAnnotationsCopy);
			  }
			  */
			  updateRings = true;
			  p.scale(WIDTH1/width, HEIGHT1/height);
			  p.redraw();
			  return;
		  }
		}
	public void mousePressedEvent(PApplet p)
	{
		float innerRadius1 = (HEIGHT1/p.height)*innerRadius; 
		float mouseX1 = (WIDTH1/p.width)*p.mouseX;
		float mouseY1 = (HEIGHT1/p.height)*p.mouseY;
		//float xTranslate1 = (WIDTH1/p.width)*xTranslate;
		p.scale(p.width/WIDTH1,p.height/HEIGHT1);
		if(newAnnotationPopup)
		{
			if((mouseY1 - HEIGHT1/2 < 8) && (mouseY1 - HEIGHT1/2 > -innerRadius1 + 250))
			{
				for(int i = 0; i< groupCutoff.length; i++)
				{
					float x = -innerRadius1 + 200f + .01f*groupCutoff[i]*(innerRadius1*2f - 400f) + WIDTH1/2 - xTranslate1;
					if((mouseX1 < x+5) && (mouseX1 > x-5))
					{
						activeSlider = i;
					}
				}
			}
		}
		  //tests if user selects a protein  	 
		  for(Protein protein: Protein.proteinList)
		  {
			  if(protein.showMe) protein.checkMouseOver();
		  }
		  //check variable table 
		  if(mouseX1 > WIDTH1 - 220 && mouseX1 < WIDTH1 - 40)
			  if(mouseY1 < HEIGHT1/2 + 500 && mouseY1 > HEIGHT1/2 - 482)
			  {
				  int pos = (int)(mouseY1 - HEIGHT1/2);
				  for(Protein protein: Protein.proteinList)
				  {
					  if(protein.type.equals("Group") && !protein.name.equals("All Groups"))
						  if(pos > protein.tablePosition - 20 && pos < protein.tablePosition + 20 )
						  {							  
							    protein.isSelected = !protein.isSelected;
								if(Main1.activeItem instanceof Protein) 
								{
									Protein p1 = (Protein) Main1.activeItem;
									if(p != null) 
									{
										p1.isActiveItem = false;
									}
								}
								Main1.activeItem = protein;
								protein.isActiveItem = true;
								protein.expand = !protein.expand;
								for(Protein protein2: Protein.proteinList)
									if(protein2.type.equals("Group")) protein2.showMe = false;							
								p.redraw();
								return;
						  }
				  }
			  }
		  for(MoveObject m: inactiveItems)
			  m.checkMouseOver();
		  
			//tests if user selects more for the popup
		  if ((selectedGene != null) && (mouseX1 - WIDTH1/2 + xTranslate1 > selectedGene.x1) && (mouseX1 - WIDTH1/2 + xTranslate1 < selectedGene.x1 + 40))
			  if((mouseY1 - HEIGHT1/2 > selectedGene.y1 + 35) && (mouseY1 - HEIGHT1/2 < selectedGene.y1 + 55))
			  {
				 expandPopup = true;
				 p.scale(WIDTH1/width, HEIGHT1/height);
				 p.redraw();
				 return;
			  }
		  if ((selectedGene != null) && (mouseX1 - WIDTH1/2 + xTranslate1 + 155 > selectedGene.x1) && (mouseX1 - WIDTH1/2 + xTranslate1 < selectedGene.x1 + 200))
			  if((mouseY1 - HEIGHT1/2 > selectedGene.y1 + 35) && (mouseY1 - HEIGHT1/2 < selectedGene.y1 + 55))
			  {
				 expandPopup = true;
			     isolate = true;
			     p.scale(WIDTH1/width, HEIGHT1/height);
			     redraw();
			     return;
			  }
		  if ((selectedGene != null) && (mouseX1 - WIDTH1/2 + xTranslate1+ 183 > selectedGene.x1) && (mouseX1 - WIDTH1/2 + xTranslate1< selectedGene.x1 + 200))
			  if((mouseY1 - HEIGHT1/2 > selectedGene.y1 ) && (mouseY1 - HEIGHT1/2 < selectedGene.y1 + 20))
			  {
				 selectedGene.isSelected = false;
				 selectedGene = null;
				 p.scale(WIDTH1/width, HEIGHT1/height);
				 p.redraw();
				 return;
			  }
		//tests if user selects option for the annotation popup
		  if((annotationPopup != null) && (mouseY1 - HEIGHT1/2 > annotationPopup.labelY - 40 + 65) && (mouseY1 - HEIGHT1/2 < annotationPopup.labelY - 40 + 85))
		  {
			  if(mouseX1 - WIDTH1/2 + xTranslate1 > annotationPopup.labelX + 40 && mouseX1 - WIDTH1/2 + xTranslate1 < annotationPopup.labelX + 140 )
			  {
				  for(Gene gene: annotationPopup.genes)
				  {
					  gene.active = !gene.active;
					  gene.activeAnnotation = annotationPopup;
				  }
				  p.scale(WIDTH1/width, HEIGHT1/height);
				  p.redraw();
				  return;
			  }
			  
			  if(mouseX1 - WIDTH1/2 + xTranslate1 > annotationPopup.labelX + 140 && mouseX1 - WIDTH1/2 + xTranslate1 < annotationPopup.labelX + 240 )
			  {
				  Annotations annotationPopupCopy = annotationPopup;
				  if(annotationPopup.level > annotationPopup.myLevels)
					  annotationPopup = new Annotations(annotationPopup, annotationPopup.myLevels + 1);		
				  else
					  annotationPopup = new Annotations(annotationPopup, annotationPopup.myLevels - 1);
				  for(int i = 0; i< annotationPopup.parentAnnotations.numChildren; i++)
					{
						if (annotationPopupCopy.parentAnnotations.children[i] == annotationPopupCopy) 
						{
							annotationPopupCopy.parentAnnotations.children[i] = annotationPopup;
							//annotationPopup.drawGroup(annotationPopup.parentAnnotations.bounds[i][0], annotationPopup.parentAnnotations.bounds[i][1], annotationPopup.myLevels, "All");
						}
					}
				  updateRings = true;
				  p.scale(WIDTH1/width, HEIGHT1/height);
				  p.redraw();
				  return;
			  }
		  }
		  
		 // float lengthFromOrigin = Main1.dist(mouseX1,mouseY1,WIDTH1/2-xTranslate1,HEIGHT1/2);
		  float lengthFromOrigin = Main1.dist(p.mouseX,p.mouseY,p.width/2-xTranslate,p.height/2);
		  
		  //checks if user is clicking in the annotations rings
		  if (lengthFromOrigin > innerRadius && lengthFromOrigin < outerRadius)
		  {
			  float mouseAngle = (float)(Math.atan2(-1*(p.mouseY - p.height/2),(p.mouseX - p.width/2 + xTranslate)));
			  if (mouseAngle< 0) mouseAngle += 2*PI;
			  if (mouseAngle< PI/2) mouseAngle += 2*PI;
			  float midRadius = (outerRadius - innerRadius1)/2 + innerRadius1;
			  float midRadiusLevel2 = (outerRadius - innerRadius1)/4 + innerRadius1;
			  float midRadiusLevel3 = (outerRadius - innerRadius1)/8 + innerRadius1;
			  int sliceLevels;
			  
			  if(activeItem != null) activeItem.unSelect();
			  if(annotationPopup != null) 
			  {
				  Annotations annotationPopupCopy = annotationPopup;
				  for(int i = 0; i< annotationPopup.parentAnnotations.numChildren; i++)
					{
						if (annotationPopupCopy.parentAnnotations.children[i] == annotationPopupCopy) 
						{
							annotationPopupCopy.parentAnnotations.children[i] = annotationPopup;
							annotationPopupCopy.parentAnnotations.children[i].isActiveItem = true;
						}
					}
					  activeItem = annotationPopup;
					  if(annotationPopup.parentAnnotations != null) annotationPopup.parentAnnotations.unSelect();
			  }
			  
			  for( int i = 0; i< firstAnnotation.numChildren; i++)
			  {
				  if (mouseAngle > firstAnnotation.bounds[i][0] && mouseAngle < firstAnnotation.bounds[i][1])
			      {
					  sliceLevels = firstAnnotation.children[i].myLevels;
					  if (sliceLevels == 1 || lengthFromOrigin > midRadius)
					  {
						  if(!(annotationPopup == null) && annotationPopup == firstAnnotation.children[i]) annotationPopup = null;
						  else annotationPopup = firstAnnotation.children[i];	
					  }
					  else 
					  {
						  for( int j = 0; j< firstAnnotation.children[i].numChildren; j++)
						  {
							  if (mouseAngle > firstAnnotation.children[i].bounds[j][0] && mouseAngle < firstAnnotation.children[i].bounds[j][1])							 
							  {				
								  sliceLevels = firstAnnotation.children[i].children[j].myLevels;
								  if (sliceLevels == 2 || lengthFromOrigin > midRadiusLevel2)
							      {
									  if(!(annotationPopup == null) && annotationPopup == firstAnnotation.children[i].children[j]) annotationPopup = null;
									  else annotationPopup = firstAnnotation.children[i].children[j];	
							      }							  
								  else
								  {
									  for( int k = 0; k< firstAnnotation.children[i].children[j].numChildren; k++)
									  {
										  if (mouseAngle > firstAnnotation.children[i].children[j].bounds[k][0] && mouseAngle < firstAnnotation.children[i].children[j].bounds[k][1])
										  {
											  sliceLevels = firstAnnotation.children[i].children[j].children[k].myLevels;
											  if (sliceLevels == 3 || lengthFromOrigin > midRadiusLevel3) 
										      {
												  if(!(annotationPopup == null) && annotationPopup == firstAnnotation.children[i].children[j].children[k]) annotationPopup = null;
												  else annotationPopup = firstAnnotation.children[i].children[j].children[k];	
										      }
											  else
											  {
												  for( int l = 0; l< firstAnnotation.children[i].children[j].children[k].numChildren; l++)
												  {
													  if (mouseAngle > firstAnnotation.children[i].children[j].children[k].bounds[l][0] && mouseAngle < firstAnnotation.children[i].children[j].children[k].bounds[l][1])
													  {
														  if(!(annotationPopup == null) && annotationPopup == firstAnnotation.children[i].children[j].children[k].children[l]) annotationPopup = null;
														  else annotationPopup = firstAnnotation.children[i].children[j].children[k].children[l];	
													  }
												  }
											  }
										  }
									  }
								  }
							  }
						  }
					  }
			      }
			  }
			  if(annotationPopup != null) 
			  {
				  Annotations annotationPopupCopy = annotationPopup;
				  for(int i = 0; i< annotationPopup.parentAnnotations.numChildren; i++)
					{
						if (annotationPopupCopy.parentAnnotations.children[i] == annotationPopupCopy) 
						{
							annotationPopupCopy.parentAnnotations.children[i] = annotationPopup;
							annotationPopupCopy.parentAnnotations.children[i].isActiveItem = true;
						}
					}
				  activeItem = annotationPopup;
				  if(annotationPopup.parentAnnotations != null) annotationPopup.parentAnnotations.unSelect();
			  }
			  p.redraw();
			  return;
		  }	 
		  
		if(mouseButton == RIGHT)
		{
			loop = true;
			//movementHandler();
			p.loop();
		}
		else
		{
			p.noLoop();
			loop = false;
		}
	}

	public void mouseDraggedEvent(PApplet p)
	{	
		float innerRadius1 = (HEIGHT1/p.height)*innerRadius; 
		float mouseX1 = (WIDTH1/p.width)*p.mouseX;
		float mouseY1 = (HEIGHT1/p.height)*p.mouseY;
		float xTranslate1 = (WIDTH1/p.width)*xTranslate;
		p.scale(p.width/WIDTH1,p.height/HEIGHT1);
		if(newAnnotationPopup && activeSlider >= 0)
		{
			groupCutoff[activeSlider] =(int)((mouseX1 - WIDTH1/2 + xTranslate1 + innerRadius1 - 200f)/(innerRadius1*2f - 400f)*100f);
			if(groupCutoff[activeSlider] > 100) groupCutoff[activeSlider] = 100;
			if(groupCutoff[activeSlider] < 0) groupCutoff[activeSlider] = 0;
		}
		if(mouseX1 > WIDTH1-50)
			if(mouseY1 < HEIGHT1/2 + 500 && mouseY1 > HEIGHT1/2 - 482)
			{
				scrollBar = (int)(100*(mouseY1-HEIGHT1/2 + 482)/982);
			}
			
		loop = true;
		//movementHandler();
		p.loop();
	}
	public void mouseReleasedEvent(PApplet p)
	{
		float innerRadius1 = (HEIGHT1/p.height)*innerRadius; 
		float mouseX1 = (WIDTH1/p.width)*p.mouseX;
		float mouseY1 = (HEIGHT1/p.height)*p.mouseY;
		float xTranslate1 = (WIDTH1/p.width)*xTranslate;
		p.scale(p.width/WIDTH1,p.height/HEIGHT1);
		if( activeItem instanceof Protein)
		{
			Protein active = (Protein) activeItem;
			if((mouseX1 < 200) && (mouseY1 < 200) && ringProtein != active)
			{
				ringProtein = active;
				updateRings = true;
				Annotations firstAnnotationsCopy = firstAnnotation;
				firstAnnotation = new Annotations(annotationOrder[0],"All", "All", firstAnnotationsCopy.genes, p, 1);
				p.redraw();
				return;
			}
			if((mouseX1 < 300) && (mouseY1 >HEIGHT1 - 200))				
			{
				if(activeItem != null)
				{
					newAnnotationGroup = active;
					newAnnotationPopup = true;
					p.scale(WIDTH1/p.width, HEIGHT1/p.height);
					((Main1)(p)).popups.newAnnotationPopup(p);
					p.scale(p.width/WIDTH1,p.height/HEIGHT1);
					Protein.drawLines = false;
					p.redraw();
				}
			}
			if(!active.type.equals("Group"))
				for(Protein pr: Protein.proteinList)
				{
					if(pr != active && !pr.type.equals("Group") && pr.showMe)
					{
						if(dist(pr.x1, pr.y1, active.x1, active.y1) < pr.height)
						{
							b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = b10 = false;
							bool1 = pr;
							bool2 = active;
							p.scale(WIDTH1/p.width, HEIGHT1/p.height);
							((Main1)(p)).popups.booleanGroupPopup(p);
							p.scale(p.width/WIDTH1,p.height/HEIGHT1);
							booleanGroupPopup = true;
						}
					}
					else if(mouseX1 > WIDTH1 - 220)
						if(dist(pr.x1, pr.y1, active.x1, active.y1) < pr.height)
						{
							p.scale(WIDTH1/p.width, HEIGHT1/p.height);
							active.isSet = false;
							Protein.drawProteins();
						}
				}
		}
		if(frontEnd) activeItem = null;
		p.noLoop();
		loop = false;
		p.cursor(Main1.ARROW);
		
	}

	public void keyPressedEvent(PApplet p)
	{
		//number keys change the eigenarray
		//left and right increment or decrement the eigenarray
		//up and down increase or decrease the cutoff
		//c, h, and m change the dataset
		boolean update  = true;
		if (p.key == Main1.CODED) {
		    if (p.keyCode == Main1.UP)	
		    {
		    	if(activeItem instanceof Protein)
		    	{
		    		if(((Protein) activeItem).myOpacity < 250)
		    			((Protein) activeItem).myOpacity = (((Protein) activeItem).myOpacity + 5);
		    	}	
		    	else if(currentCutoff.equals("pValue")) pValueCutoff = pValueCutoff*2;
		    	else if(currentCutoff.equals("Beta")) bundlingStrength += .02;
		    	else 
		    	{
		    		expressionCutoff = Math.round((expressionCutoff + expressionCutoff*.1)*100000.0)/100000.0;
		    		if(expressionCutoff < .00005f) expressionCutoff = .0001f;
		    	}
		    		
		    	update  = false;
		    }
		    else if (p.keyCode == Main1.DOWN)  	
		    {
		    	if(activeItem instanceof Protein)
		    	{
		    		if(((Protein) activeItem).myOpacity > 5)
		    			((Protein) activeItem).myOpacity = (((Protein) activeItem).myOpacity - 5);
		    	}	
		    	else if(currentCutoff.equals("pValue")) pValueCutoff = pValueCutoff/2;
		    	else if(currentCutoff.equals("Beta")) bundlingStrength -= .02;
		    	else
		    	{
		    			expressionCutoff = Math.round((expressionCutoff - expressionCutoff*.1)*100000.0)/100000.0;
		    			//if(expressionCutoff < .0005f) expressionCutoff = 0f;
		    	}
		    	update  = false;
		    }
		    else if (p.keyCode == Main1.RIGHT)  
		    	if(activeItem instanceof Protein)
		    	{
		    		((Protein) activeItem).numInGroup = (((Protein) activeItem).numInGroup + 1) % 10;
		    	}
		    else if (p.keyCode == Main1.LEFT)  
		    	if(activeItem instanceof Protein)
		    	{
		    		((Protein) activeItem).numInGroup = (((Protein) activeItem).numInGroup + 1) % 10;
		    	}	
		}
		
		if(p.key == 'x') 
		{
			proteinGroup = (proteinGroup + 1) % 6;
			showProteins = proteinGroups[proteinGroup];
			update  = false;
		}
		/*
		if(p.key == 'p') 
		{
			proteinBinding = !proteinBinding;
			if(!proteinBinding) 
			{
				double temp = pValueCutoff;
				if(oldCutoff > 0) expressionCutoff = oldCutoff;
				else expressionCutoff = .002;
				oldCutoff = temp;				
			}
			else 
			{
				double temp = expressionCutoff;
				if(oldCutoff > 0) pValueCutoff = oldCutoff;
				else pValueCutoff = .01;
				oldCutoff = temp;
			}
			update  = true;
		}
		*/
		if(p.key == 'l') 
		{
			  if(firstAnnotation.myLevels < 4)
				  firstAnnotation = new Annotations(firstAnnotation, firstAnnotation.myLevels + 1);		
			  else
			  {
				  firstAnnotation = new Annotations(firstAnnotation, 1);
				  LEVELS = 1;
			  }
			  
			  updateRings = true;
			  p.redraw();
			  return;
		}
		if(p.key == 'r') 
		{
			Annotations firstAnnotationsCopy = firstAnnotation;
			firstAnnotation = new Annotations(annotationOrder[0],"All", "All", Gene.geneList, p, 1);
			Annotations.setLevels(firstAnnotation, firstAnnotationsCopy);
			//for(MoveObject m: inactiveItems)
				//if(m instanceof Annotations) inactiveItems.remove(m);
		}
		if(p.key == 'v' && activeItem instanceof Protein)
		{
			Protein pr = (Protein) activeItem;
			pr.numInGroup = pr.numInGroup + 1;
		}
		if(p.key == 'q')
		{
			if(currentCutoff.equals("pValue")) currentCutoff = "Expression";
			else if (currentCutoff.equals("Expression")) currentCutoff = "Beta";
			else currentCutoff = "pValue";
		}
		if(p.key == ',')
		{
			angleOffset = angleOffset - .05f;
			updateRings = true;
		}
		if(p.key == '.')
		{
			angleOffset = angleOffset + .05f;
			updateRings = true;
		}
		if(p.key == 'b' && annotationPopup != null)
		{
			for(Gene gene: annotationPopup.genes)
			{
				System.out.println(gene.name);
			}
		}
		if(p.key == 'v')
		{
			for(Gene gene: firstAnnotation.genes)
			{
				System.out.println(gene.name);
			}
		}
		if(p.key == '1')
		{
			p.resize(2000,1435);
			resized = true;
		}
		if (p.key == '2') 
		{
			p.resize(1024,768);
			resized = true;
		}
		if (p.key == '3') 
		{
			p.resize(screen.width,screen.height);
			resized = true;
		}
		
		if(update) updateRings  = true;
		p.redraw();
		resized = true;
	}


}
